package com.adb.androidwebalbum.uc;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.adb.androidwebalbum.R;
import com.adb.androidwebalbum.bean.ImageBean;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ImageGalleryAdapter extends BaseAdapter{
	
	private LayoutInflater mInflater;
	private Activity mActivity;
	private ImageLoader mImageLoader; 
	private DisplayImageOptions mOptions;
	private ArrayList<ImageBean> mThumnailList;
	
    public static class ViewHolder{
        public ImageView mImageView;
    }
    
	public ImageGalleryAdapter(Activity mActivity,ImageLoader mImageLoader,DisplayImageOptions mOptions) {
		this.mActivity = mActivity;
		this.mImageLoader = mImageLoader;
		this.mOptions = mOptions;
		mThumnailList = new ArrayList<ImageBean>();
		mInflater = (LayoutInflater)(this.mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
	}
	
	public void addItemArrayList(ArrayList<ImageBean> mArrayListBean){
		if(mArrayListBean == null)
		return;
		if(!mThumnailList.isEmpty()){
			mThumnailList.clear();
		}
		for(ImageBean mBeanofImage : mArrayListBean){
			mThumnailList.add(mBeanofImage);
		}
		notifyDataSetChanged();
	}
	
	public void removeAllIteams(){
		if(mThumnailList != null && mThumnailList.size() > 0)
		{
			mThumnailList.clear();
			notifyDataSetChanged();
			
		}
	}
	
	public int getCount() {
		return mThumnailList.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}	
	 // create a new ImageView for each item referenced by the Adapter
	public View getView(int position, View convertView, ViewGroup parent) {
		
			ViewHolder mHolder;
		 	View mView = convertView;
	        if (mView == null) {  // if it's not recycled, initialize some attributes
	        	mView = mInflater.inflate(R.layout.item_image_gallery_adapter, parent, false);
	        	mHolder = new ViewHolder();
	        	mHolder.mImageView = (ImageView)mView.findViewById(R.id.imagView_Photo);
	        	mView.setTag(mHolder);
	        } else {
	        	mHolder = (ViewHolder)mView.getTag();
	        }

	        ImageBean mBeanofImage = mThumnailList.get(position);
	        mImageLoader.displayImage(mBeanofImage.getThumbpath(),mHolder.mImageView,mOptions);
	      
	        
	   return mView;
	}
	
}
